﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using System.Configuration;
using MaxStudio.Concrete.Component;
using System.Collections;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class RPTSchoolConcreteTest : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
                //BindDDLDays();
            }
        }
        #region Private Method


        //private void BindDDLDays()
        //{
        //    ddlDays.Items.Clear();
        //    ddlDays.DataSource = FillDropDownList.EnumListTable(typeof(EnumSchoolDays));
        //    ddlDays.DataValueField = "value";
        //    ddlDays.DataTextField = "text";
        //    ddlDays.DataBind(); 

        //}

        private void BindGridView()
        {
            RPTSchoolConcreteTestService RPTSchoolConcreteTestServiceObj = ServiceFactory.getRPTSchoolConcreteTestService();
            OperationResult<List<tb_RPT_SchoolConcreteTest>> result = RPTSchoolConcreteTestServiceObj.GetAllSchoolConcreteTest();

            if (result.operationSuccess)
            {
                gvSchoolConcreteTestList.DataSource = result.obj;
                gvSchoolConcreteTestList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            RPTSchoolConcreteTestService RPTSchoolConcreteTestServiceObj = ServiceFactory.getRPTSchoolConcreteTestService();
            OperationResult<tb_RPT_SchoolConcreteTest> resultEdit = RPTSchoolConcreteTestServiceObj.GetSchoolConcreteTestById(id);

            if (resultEdit.operationSuccess == true)
            {
                //txtEditWeather.Text = resultEdit.obj.Weather;
                txtEditProjectName.Text = resultEdit.obj.ProjectName ;
                txtEditStrengtGrade.Text = resultEdit.obj.StrengtGrade ;
                txtEditInputDate3Day.Text = resultEdit.obj.InputDate3Day ;
                txtEditClearedPeople3Day.Text = resultEdit.obj.ClearedPeople3Day ;
                txtEditInputDate7Day.Text = resultEdit.obj.InputDate7Day ;
                txtEditClearedPeople7Day.Text = resultEdit.obj.ClearedPeople7Day ;

                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RPTSchoolConcreteTestService RPTSchoolConcreteTestServiceObj = ServiceFactory.getRPTSchoolConcreteTestService();
            tb_RPT_SchoolConcreteTest instance = new tb_RPT_SchoolConcreteTest(); 

            instance.ProjectName = txtProjectName.Text;
            instance.StrengtGrade = txtStrengtGrade.Text;
            instance.InputDate3Day = txtInputDate3Day.Text;
            instance.ClearedPeople3Day = txtClearedPeople3Day.Text;
            instance.InputDate7Day = txtInputDate7Day.Text;
            instance.ClearedPeople7Day = txtClearedPeople7Day.Text;

            OperationResult<tb_RPT_SchoolConcreteTest> result = RPTSchoolConcreteTestServiceObj.CreateSchoolConcreteTest(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            RPTSchoolConcreteTestService RPTSchoolConcreteTestServiceObj = ServiceFactory.getRPTSchoolConcreteTestService();
            tb_RPT_SchoolConcreteTest entity = new tb_RPT_SchoolConcreteTest();

            entity.Id = editId;  
            entity.ProjectName = txtEditProjectName.Text;
            entity.StrengtGrade = txtEditStrengtGrade.Text;
            entity.InputDate3Day = txtEditInputDate3Day.Text;
            entity.ClearedPeople3Day = txtEditClearedPeople3Day.Text;
            entity.InputDate7Day = txtEditInputDate7Day.Text;
            entity.ClearedPeople7Day = txtEditClearedPeople7Day.Text;

            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_RPT_SchoolConcreteTest> resultEdit = RPTSchoolConcreteTestServiceObj.ModifySchoolConcreteTest(entity); 
            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            RPTSchoolConcreteTestService RPTSchoolConcreteTestServiceObj = ServiceFactory.getRPTSchoolConcreteTestService();
            tb_RPT_SchoolConcreteTest deleteEntity = new tb_RPT_SchoolConcreteTest();
            deleteEntity.Id = deleteId;
            RPTSchoolConcreteTestServiceObj.DeleteSchoolConcreteTest(deleteEntity);

            BindGridView();
        }

        protected void gvSchoolConcreteTestList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvSchoolConcreteTestList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSchoolConcreteTestList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
